local clsTestLayer = Class("lua/game/clsSceneLayer.lua"):Inherit()
local oObjectFactory = Class("lua/game/clsObjectFactory.lua"):getInstance()
local oSkillCDManager = Class("lua/game/combat/clsSkillCDManager.lua"):getInstance()
function clsTestLayer:init(config)
	oSkillCDManager:clearSkillCD()
	oSkillCDManager:addSkill({400100, 400200, 400300, 400400, 401300, 401500, 401600})
	local UIWidget = createObject(self.uiLayer,{res = "skills_button/NewUibutton_1.ExportJson", objectType = "widget"},self)
	local Button_4 = createObject(UIWidget, {objectType = "ccsui", uiType = "Button", key = "Button_4", event = {up = self.heroAttack}},self)
	self.Button_6 = createObject(UIWidget, {objectType = "ccsui", uiType = "Button", key = "Button_6", event = {up = self.heroSkill_1}},self)
	--self.progressTimer1 = createUIProgressTimer(nil, {res = "progressTimer.png",z = 50})
	--Button_6:addChild(self.progressTimer1, 91, 0, 0)
end

function clsTestLayer:heroSkill_1()
	local ret = false
	if not oSkillCDManager:isSkillCooling(400400) then
		oSkillCDManager:doCooling(400400)
		--self:runProgressTimer(self.progressTimer1, 400400, self.Button_6)
		self.hero:skillButtonCallBack(400400)
		self.hero:endRun()
		ret = true
	end
	return ret
end

function clsTestLayer:heroAttack()
	
	self.hero:attackButtonCallBack()
end

function clsTestLayer:moveMainRole(dt)
	local step = self.step*dt*40
	self:doMoveRole(step)
end

function clsTestLayer:onFrameEvent(dt)
	self:moveMainRole(dt)
end

function clsTestLayer:doMoveRole(step)
	local currentX,currentY = self.hero:getPosition()
	if not currentX or not currentY then
		return
	end
	if currentX - self.heroSize.width / 2  <= self.leftLimit then
		currentX = self.heroSize.width / 2 + self.leftLimit
	elseif currentX + self.heroSize.width / 2  >= self.rightLimit then
		currentX = self.rightLimit - self.heroSize.width / 2
	end
	if currentY <= self.bottomLimit then
		currentY = self.bottomLimit
	elseif currentY >= self.topLimit then
		currentY = self.topLimit
	end
	if self.oJoystick.joystickSwitch == true then
		local nextX = currentX + step * self.oJoystick.angle.peopleCos
		local nextY = currentY + step * self.oJoystick.angle.peopleSin
		
		if currentX - self.heroSize.width / 2 + step * self.oJoystick.angle.peopleCos <= self.leftLimit then
			nextX = self.heroSize.width / 2 + self.leftLimit
		elseif currentX + self.heroSize.width / 2 + step * self.oJoystick.angle.peopleCos >= self.rightLimit then
			nextX = self.rightLimit - self.heroSize.width / 2
		end
		
		if currentY + step * self.oJoystick.angle.peopleSin <= self.bottomLimit then
			nextY = self.bottomLimit
		elseif currentY + step * self.oJoystick.angle.peopleSin  >= self.topLimit then
			nextY = self.topLimit
		end
		if self.oJoystick.angle.peopleCos then
			if self.oJoystick.angle.peopleCos >= 0 then
				self.hero:turnRight()
			else
				self.hero:turnLeft()
			end
		end
		self.hero:run("walk", {x = nextX, y = nextY, peopleCos = self.oJoystick.angle.peopleCos})
	elseif not self.isFindingPath and  not self.isAIControl then
		self.hero:run("stand")
	end
	
	if self.mapLayerSwitch then
		self:moveLayer()
	end
end

function getClass()
	return clsTestLayer
end
